﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RuleCode.Table;
using RuleCode.Common;
using System.Data;
using System.Data.SqlClient;
using System.Net;

namespace chatblog.usercate
{
    public partial class chRoom : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
              AjaxPro.Utility.RegisterTypeForAjax (typeof (chRoom ));
              cbuser.dealcookie();
              if (AliApi.appUserId != string.Empty && AliApi.appUserId != null)
              {
                  cbuser user = cbuser.Find("cbuserid='" + AliApi.appUserId + "'");
                  if (user.Count > 0)
                  {
                      if (IsPostBack == false && user.chatIsOnLine.Value == RuleCode.Enum.BooleanEnum.Yes)
                      {
                          string[] chatAddressResult = resolveChatAddress(user.cbChatAddress.Value);
                          string scriptStr = string.Empty;
                          scriptStr = "alert('对不起，您的当前用户已经在其他地方打开该聊天室！\n如果不是您自己的行为，请您及时修改密码，以防密码丢失！');document.location .href='ChatSetup.aspx?redirect=1'; ";
                          ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "hasLogin", "<script type=\"text/javascript\">" + scriptStr + "</script>", false);
                          return;
                      }
                      else
                      {
                          //IPAddress[] ipAddressList = Dns.GetHostAddresses (Dns.GetHostName());
                          string ip = Request.UserHostAddress; //ipAddressList[0].ToString();
                          string hostName = Request .UserHostName ;//Dns.GetHostName();
                          string currentDateTime = DateTime.Now.ToString ();
                          user.cbChatAddress.Value = "ip=" + ip + ";time=" + currentDateTime+";host="+hostName ;
                          user.chatIsOnLine.Value = RuleCode.Enum.BooleanEnum.Yes;
                          user.isonline.Value = RuleCode.Enum.BooleanEnum.Yes;
                          user.Modify();
                          AliApi.chatOpenTime = currentDateTime;
                      }
                  }
              }
              else 
              {
                  Response.Redirect(ResolveUrl("chatLogin.aspx") + "?redirecturl=" + this.Request.Url);
              }
              if (!IsPostBack)
              {
                  BindUserCate();
              }
        }
                          
        /// <summary>
        /// 解析数据库中保存的 聊天室的地址和打开时间
        /// </summary>
        /// <param name="chatAddress"></param>
        /// <returns></returns>
        private string[] resolveChatAddress(string chatAddress)
        {
            string[] chatAddressResult =new string[3];
            string[] arg0 = chatAddress.Split(";".ToCharArray ());
            if (arg0.Length >= 3)
            {
                string[] ipArgs = arg0[0].Split("=".ToCharArray());
                chatAddressResult[0] = ipArgs[1];
                string[] timeArgs = arg0[1].Split("=".ToCharArray());
                chatAddressResult[1] = timeArgs[1];
                string[] hostArgs = arg0[2].Split("=".ToCharArray());
                chatAddressResult[2] = hostArgs[1];
            }
            return chatAddressResult;
        }

        #region 当前用户相关
        [AjaxPro.AjaxProperty]
        public string currentUserID
        {
            get
            {
                return AliApi.appUserId;
            }

        }

        [AjaxPro.AjaxMethod]
        public string GetCurrentUserID()
        {
            return currentUserID;
        }

        private static string systemUserId = "系统消息";
        #endregion

        #region 绑定分组和右键菜单
        public void BindUserCate()
        {
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "currentuser", "<script type=\"text/javascript\">currentUser = chatblog.usercate.chRoom.GetCurrentUserID().value;</script>", false);
            if (currentUserID == null || currentUserID == "")
                return;

            this.DoorP.InnerHtml = GetDoorPInnerHtml ();

        }
        #endregion

        #region 好友分组Ajax方法
        /// <summary>
        /// 删除组内用户
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="groupid"></param>
        [AjaxPro.AjaxMethod]
        public void DeleteUser(string userid, string groupid)
        {
            cbusergrouplist grouplist = cbusergrouplist.Find("cbuserid='" + userid + "' and cbusergroupid='" + groupid + "'");
            if (grouplist.Count > 0)
            {
                grouplist.CurrentIndex = 0;
                grouplist.Delete();
            }
        }

        /// <summary>
        /// 获取右键菜单 HTML代码
        /// </summary>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public string GetDoorPInnerHtml()
        {
            string html = string.Empty;
            string childCate = string.Empty;
            string childUser = string.Empty;
            string groupListFilter = string.Empty;

            RuleCode.Table.cbusergroup userGroup = cbusergroup.Find("cbuserid='" + currentUserID + "'", "sort_order desc");
            if (userGroup.Count > 0)
            {
                do
                {
                    string sql = "[dbo].[GetUserGroupList] '" + userGroup.cbusergroupid.Value + "'";
                    DataSet dSet = SqlHelper.Instance.ExecuteQuery(sql);
                    int count = dSet.Tables[0].Rows.Count;
                    childCate = "<h2  class=\"h2style\" id=\"" + userGroup.cbusergroupid.Value + "\">" + userGroup.cbname.Value + "<a>(" + count.ToString() + ")</a></h2>";
                    childCate += "<div class=\"content \">";
                    if (dSet.Tables.Count > 0 && dSet.Tables[0].Rows.Count > 0)
                    {

                        foreach (DataRow dRow in dSet.Tables[0].Rows)
                        {
                            string imgStr = string.Empty;
                            if (dRow["chatIsOnLine"] == null || dRow["chatIsOnLine"].ToString() == "")
                            {
                                imgStr = "<img alt=\"未注册\" title=\"暂时还未注册成系统用户\"  src =\"img/notuser.ico\" />";
                                childCate += "<span class=\"contextMenu0\"  id='" + dRow["g_cbusergroupid"].ToString() + "'title=\"" + dRow["g_cbuserid"].ToString()
                                + "\" ondblclick=\"alert('对不起，该用户还未注册为系统用户，不能聊天！');\">"
                                + imgStr + dRow["g_cbname"].ToString() + "<input value=\"" + dRow["g_cbuserid"].ToString() + "\" type=\"hidden\" /></span>" + "<br />";
                            }
                            else if (dRow["chatIsOnLine"].ToString() == "1")
                            {
                                imgStr = "<img alt=\"在线\" title=\"该用户在线\"  src =\"img/regular_smile.gif\" />";
                                childCate += "<span class=\"contextMenu0\"   id='" + dRow["g_cbusergroupid"].ToString() + "'title=\"" + dRow["g_cbuserid"].ToString()
                                + "\" ondblclick=\"addChatRoom('" + dRow["g_cbusergroupid"] + "','" + dRow["RECID"].ToString() + "','" + dRow["g_cbuserid"].ToString() + "');removeImgTimeIdByUserId('" + dRow["g_cbuserid"].ToString() + "');\">"
                                + imgStr + dRow["g_cbname"].ToString() + "<input value=\"" + dRow["g_cbuserid"].ToString() + "\" type=\"hidden\" /></span>" + "<br />";
                            }
                            else if (dRow["chatIsOnLine"].ToString() == "0")
                            {
                                imgStr = "<img alt=\"不在线\" title=\"该用户不在线\"  src =\"img/regular_smile_nocolor.gif\" />";
                                childCate += "<span class=\"contextMenu0\"   id='" + dRow["g_cbusergroupid"].ToString() + "'title=\"" + dRow["g_cbuserid"].ToString()
                                + "\" ondblclick=\"addChatRoom('" + dRow["g_cbusergroupid"] + "','" + dRow["RECID"].ToString() + "','" + dRow["g_cbuserid"].ToString() + "');removeImgTimeIdByUserId('" + dRow["g_cbuserid"].ToString() + "');\">"
                                + imgStr + dRow["g_cbname"].ToString() + "<input value=\"" + dRow["g_cbuserid"].ToString() + "\" type=\"hidden\" /></span>" + "<br />";
                            }
                            else
                            {
                                imgStr = "<img alt=\"未知\" title=\"暂时未知用户\"  src =\"img/weizhi.ico\" />";
                                childCate += "<span class=\"contextMenu0\"  id='" + dRow["g_cbusergroupid"].ToString() + "'title=\"" + dRow["g_cbuserid"].ToString()
                                + "\" ondblclick=\"alert('对不起，该用户还未注册为未知用户，不能聊天！');\">"
                                + imgStr + dRow["g_cbname"].ToString() + "<input value=\"" + dRow["g_cbuserid"].ToString() + "\" type=\"hidden\" /></span>" + "<br />";
                            }
                        }
                    }
                    childCate += "</div>";
                    html += childCate;
                } while (userGroup.Next());
            }

            return html;
        }

        /// <summary>
        /// 获取 分组列表HTML代码
        /// </summary>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public string GetGroupListUlHtml(string userid, string oldGroupid)
        {
            string ulHtml = string.Empty;
            RuleCode.Table.cbusergroup userGroup = cbusergroup.Find("cbuserid='" + currentUserID + "'", " cbusergroupid ");
            if (userGroup.Count > 0)
            {
                do
                {
                    ulHtml += "<li id=\"" + userGroup.cbusergroupid.Value + "\" onclick=\"changeGroupId('" + userid + "','" + oldGroupid + "','" + userGroup.cbusergroupid.Value + "')\" >" + userGroup.cbname.Value + "</li>";  //onclick=\"changeGroupId('dddd')\"
                } while (userGroup.Next());
            }
            return ulHtml;
        }

        /// <summary>
        /// 获取 分组列表HTML代码
        /// </summary>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public string GetGroupListSelectHtml(string defaultGroupid)
        {
            string ulHtml = "<table><tr style =\" width :200px; \"><td>选择好友分 类：</td><td style=\"text-align:left;\"><select style =\" width:200px\" id=\"GroupListSelect\">";
            RuleCode.Table.cbusergroup userGroup = cbusergroup.Find("cbuserid='" + currentUserID + "'", " cbusergroupid ");
            if (userGroup.Count > 0)
            {
                do
                {
                    if (defaultGroupid == userGroup.cbusergroupid.Value)
                    {
                        ulHtml += "<option selected=\"true\" id=\"" + userGroup.cbusergroupid.Value + "\">" + userGroup.cbname.Value + "</option>";
                    }
                    else
                    {
                        ulHtml += "<option id=\"" + userGroup.cbusergroupid.Value + "\">" + userGroup.cbname.Value + "</option>";
                    }
                } while (userGroup.Next());
            }
            ulHtml += "</select></td></tr></table>";
            return ulHtml;
        }

        [AjaxPro.AjaxMethod ]
        public string GetGroupListHtml(string defaultGroupid)
        {
            string jsonString = string.Empty;
            RuleCode.Table.cbusergroup userGroup = cbusergroup.Find("cbuserid='" + currentUserID + "'", " cbusergroupid ");
            if (userGroup.Count > 0)
            {
                int index = 0;
                //jsonString = "{[id,name],[id,name]}";
                jsonString += "{groupList:[";
                do
                {
                    if (index > 0)
                        jsonString += ",";
                    if (defaultGroupid == userGroup.cbusergroupid.Value)
                    {
                        jsonString +="{id:\"" + userGroup.cbusergroupid.Value + "\",name:\"" + userGroup.cbname.Value + "\",selected:true}";
                        //jsonString += "<option selected=\"true\" id=\"" + userGroup.cbusergroupid.Value + "\">" + userGroup.cbname.Value + "</option>";
                    }
                    else
                    {
                        jsonString += "{id:\"" + userGroup.cbusergroupid.Value + "\",name:\"" + userGroup.cbname.Value + "\",selected:false}";
                        //jsonString += "<option id=\"" + userGroup.cbusergroupid.Value + "\">" + userGroup.cbname.Value + "</option>";
                    }
                    index++;
                } while (userGroup.Next());
                jsonString += "]}";
            }
            return jsonString;
        }

        /// <summary>
        /// 更新用户分组信息
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="oldGroupId"></param>
        /// <param name="newGroupId"></param>
        [AjaxPro.AjaxMethod]
        public void UpdateUserGroup(string userid, string oldGroupId, string newGroupId)
        {
            cbusergrouplist grouplist = cbusergrouplist.Find("cbuserid='" + userid + "' and cbusergroupid='" + oldGroupId + "'");
            if (grouplist.Count > 0)
            {
                grouplist.cbusergroupid.Value = newGroupId;
                grouplist.Modify();
            }
        }

        /// <summary>
        /// 重命名好友昵称
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="groupId"></param>
        /// <param name="newName"></param>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public bool RenameGroupUserName(string userid, string groupId, string newName)
        {
            cbusergrouplist groupList = cbusergrouplist.Find("cbusergroupid='" + groupId + "' and cbuserid='" + userid + "'");
            if (groupList.Count > 0)
            {
                groupList.cbname.Value = newName;
                groupList.Modify();
                return true;
            }
            return false;
        }

        [AjaxPro.AjaxMethod]
        public string  AddGroup(string cbName)
        {
            string result = string.Empty;
            if (AliApi.appUserId != string.Empty)
            {
                cbusergroup group = cbusergroup.Find("cbuserid='" + AliApi.appUserId + "' and cbname='" + cbName + "'");
                if (group.Count > 0)
                {
                    result = "新增的好友分组命名已重名！";
                }
                else
                {
                    group.New();
                    group.cbuserid.Value = currentUserID;
                    group.cbusergroupid.Value = group.GetRecID().ToString();
                    group.cbname.Value = cbName;
                    group.Insert();
                }
            }
            else
            {
                result = "您还未登录！";
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public void DeleteGroup(string groupid, string cbName)
        {
            cbusergroup group = cbusergroup.Find("cbusergroupid='" + groupid + "' and cbname='" + cbName + "'");
            if (group.Count > 0)
            {
                group.CurrentIndex = 0;
                group.Delete();
            }
        }

        [AjaxPro.AjaxMethod]
        public string UpdateGroup(string groupid, string oldName, string newName)
        {
            string result = string.Empty;
            if (AliApi.appUserId != string.Empty)
            {
                cbusergroup usegroup0 = cbusergroup.Find("cbuserid='"+AliApi.appUserId +"' and cbname='"+newName +"'");
                if (usegroup0.Count > 0)
                {
                    result = "好友分组不能重名！";
                }
                else
                {
                    cbusergroup group = cbusergroup.Find("cbusergroupid='" + groupid + "' and cbname='" + oldName + "'");
                    if (group.Count > 0)
                    {
                        group.cbname.Value = newName;
                        group.Modify();
                    }
                }
            }
            else
            {
                result = "对不起，你还未登录！";
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public string AddGroupUser(string groupid, string userid, string username, string status)
        {
            try
            {
                string sql = "select * from cbusergrouplist where cbuserid='" + userid + "' and cbusergroupid in (select cbusergroupid from cbusergroup where cbuserid='" + currentUserID + "')";
                DataSet dSet = SqlHelper.Instance.ExecuteQuery(sql);
                if (dSet.Tables.Count > 0 && dSet.Tables[0].Rows.Count > 0)
                {
                    //MessageBox.Show(this.Page, "该用户已经是好友，不能添加！");
                    return "该用户已经是好友，不能添加！";
                }
                cbusergrouplist grouplist = new cbusergrouplist();
                grouplist.Init(0);
                grouplist.New();
                grouplist.cbname.Value = username;
                grouplist.cbstatus.Value = status;
                grouplist.cbusergroupid.Value = groupid;
                grouplist.cbuserid.Value = userid;
                grouplist.Insert();
            }
            catch (Exception ep)
            {
                return ep.Message;
            }
            return "";
        }

        [AjaxPro.AjaxMethod]
        public int GetGroupUserList(string groupid)
        {
            string filter = "cbusergroupid='" + groupid + "' ";
            cbusergrouplist grouplist = cbusergrouplist.Find(filter);
            return grouplist.Count;
        }
        #endregion

        /// <summary>
        /// 注销用户聊天室
        /// </summary>
        [AjaxPro.AjaxMethod]
        public void LoginOut()
        {
            if (AliApi.appUserId != null && AliApi.appUserId != string.Empty && GetCurrentChatStatus()==string .Empty )     //&& Util.useLoginOut == true
            {
                cbuser user = cbuser.Find("cbuserid='" + AliApi.appUserId + "'");
                if (user.Count > 0)
                {
                    user.chatIsOnLine.Value = RuleCode.Enum.BooleanEnum.No;
                    user.cbChatAddress.Value = "";
                    user.Modify();
                }

                AliApi.chatOpenTime = "";

                cbChatRoom chatRoom =cbChatRoom .Find ("chat_from='"+AliApi.appUserId +"'");
                if(chatRoom.Count >0)
                {
                    chatRoom.Delete("chat_from='" + AliApi.appUserId + "'");
                }

            }
        }

        [AjaxPro.AjaxMethod ]
        public string[] GetUserid(string recid, string groupid)
        {
            string[] result = new string[2];
            if (recid != string.Empty && recid != null)
            {
                string sql = "select a.cbuserid,a.cbname from dbo.cbusergrouplist as a left join dbo.cbuser as b  on a.cbuserid=b.cbuserid where b.RECID=@RECID000 and a.cbusergroupid=@cbusergroupid";
                SqlParameter[] sqlparams = new SqlParameter[2];
                sqlparams[0] = new SqlParameter("@RECID000", recid);
                sqlparams[1] = new SqlParameter("@cbusergroupid", groupid);
                DataSet dSet = SqlHelper.Instance.ExecuteQuery(sql, sqlparams);
                if (dSet.Tables.Count > 0 && dSet.Tables[0].Rows.Count > 0)
                {
                    result[0] = dSet.Tables[0].Rows[0]["cbuserid"].ToString ();
                    result[1] = dSet.Tables[0].Rows[0]["cbname"].ToString ();
                }
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public string[] GetUserid0(string user_recid)
        {
            string[] result = new string[2];
            if (user_recid != string.Empty && user_recid != null)
            {
                cbuser user = cbuser.Find("recid="+user_recid );
                if (user.Count > 0)
                {
                    result[0] = user.cbuserid.Value;
                    result[1] = user.cbusernickname .Value;
                }
            }
            return result;
        }
       
        /// <summary>
        /// 获取所有好友的状态
        /// </summary>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public string getAllUserStatus()
        {
            string result = string.Empty;
            string sqlText = "dbo.[GetChatUserStatus] '" + AliApi.appUserId + "',-1";
            DataSet dSet = SqlHelper.Instance.ExecuteQuery(sqlText);
            if (dSet.Tables.Count > 0 && dSet.Tables[0].Rows.Count > 0)
            {
                result = "{[";
                int i = 0;
                foreach (DataRow dRow in dSet.Tables[0].Rows)
                {
                    if (i++ > 0)
                        result += ",";
                    result += "{";
                    result +="user_to:'"+ dRow["userid_to"].ToString() + "',";
                    if (dRow["chatIsOnline"] == null)
                    {
                        result += "isOnline:0,";
                    }
                    else
                    {
                        result +="isOnline:"+ dRow["chatIsOnline"].ToString()+",";
                    }
                    result += "groupid:'" + dRow["cbusergroupid"].ToString() + "'";
                    result += "}";
                }                    
                result += "]}";
            }
            return result;
        }

        /// <summary>
        /// 获取用户聊天室状态
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        [AjaxPro.AjaxMethod ]
        public bool  getUserStatus(string userid)
        {
            bool online = false;
            string filter = "cbuserid=@cbuserid00";
            SqlParameter[] sqlParams = new SqlParameter[1];
            sqlParams[0] = new SqlParameter("@cbuserid00",userid);
            cbuser user = RuleCode.Common.Table .Find(filter, sqlParams,typeof (cbuser )) as cbuser ;
            if(user.Count>0)
                return online = (user.chatIsOnLine .Value == RuleCode.Enum.BooleanEnum .Yes );
            return online;
        }

        /// <summary>
        /// 获取 Guid
        /// </summary>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public string GetNewGuid()
        {
            return Guid.NewGuid().ToString("N");
        }

        /// <summary>
        /// 获取 当前用户的聊天室状态 （主要用于判断同一个用户打开多个聊天室）
        /// </summary>
        /// <returns></returns>
        [AjaxPro.AjaxMethod ]
        public string GetCurrentChatStatus()
        {
            string result = string.Empty ;   //正常
            if (AliApi.appUserId != string.Empty)
            {
                cbuser user = cbuser.Find("cbuserid='"+AliApi.appUserId +"'");
                if (user.Count > 0)
                {
                    if (user.chatIsOnLine.Value == RuleCode.Enum.BooleanEnum.Yes)
                    {
                        if (user.cbChatAddress.Value == string.Empty)
                        {
                            result = "聊天室状态异常！";
                        }
                        else
                        {
                            string[] ipTimeHost = resolveChatAddress(user.cbChatAddress.Value);
                            string hostName =HttpContext.Current . Request.UserHostName;// Dns.GetHostName();
                            //IPAddress[] ipAddressList = Dns.GetHostAddresses(hostName);
                            string ip = HttpContext.Current.Request.UserHostAddress;// ipAddressList[0].ToString();
                            if (ip == ipTimeHost[0] && AliApi.chatOpenTime == ipTimeHost[1] && hostName == ipTimeHost[2])
                            {
                                result = string.Empty;
                            }
                            else
                            {
                                result = "您的当前用户在其他地方打开了聊天室。\n 如果不是您自己的行为，请您及时修改密码后再登录！";
                            }
                        }
                    }
                    else
                    {
                        result = "聊天室异常退出！";
                    }
                }
                else
                {
                    result = "当前用户未找到，请退出系统，重新登录！";
                }
            }
            else
            {                     
                result = "您未登录！";          //系统未登录
            }
            return result;
        }

        #region 聊天室相关
        /// <summary>
        /// 获取私人聊天记录
        /// </summary>
        /// <param name="user_from">聊天对象</param>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public string GetPersonalChatInfoHtml(string user_from)
        {

            string chatinfoHtml = string.Empty;
            if (currentUserID == "")
                return chatinfoHtml;
            Int64 maxChatInfoId = 0;
            DataSet dSet = SqlHelper.Instance.ExecuteQuery("[GetChatInfo] '" + currentUserID + "', '" + user_from + "'");
            if (dSet.Tables.Count > 0 && dSet.Tables[0].Rows.Count > 0)
            {
                //chatinfoHtml ="<div id=\"MsgDiv\" style =\"  overflow :hidden ; width:100%\">";
                string childHtml = string.Empty;
                foreach (DataRow dRow in dSet.Tables[0].Rows)
                {
                    
                    string color = (dRow["user_from"].ToString() == currentUserID) ? "#308712" : "blue";
                    childHtml = "<div  class =\"chatInfoRecord\">";
                    childHtml += "<span style =\"font-size:smaller; color:" + color + "; font-weight :bold ;\">" + dRow["name_from"].ToString() + "</span> ";
                    childHtml += "<span style =\"font-size:smaller;color:Gray\">(" + dRow["sendTime"].ToString() + ")</span> <br />";
                    childHtml += "<div style =\" padding-left :20px;\">";
                    childHtml += dRow["content"].ToString();
                    childHtml += "</div>";
                    childHtml += "</div>";
                    chatinfoHtml += childHtml;

                    Int64 chatId = Convert.ToInt64(dRow["cbChatInfoId"]);
                    if (chatId > maxChatInfoId)
                        maxChatInfoId = chatId;
                }
                //chatinfoHtml += "</div>";
                cbuser.SetNewMsgPos(currentUserID, maxChatInfoId.ToString(),user_from ,false );
                cbuser.SetNewMsgPos(user_from, maxChatInfoId.ToString(), currentUserID, true);
            }
            return chatinfoHtml;
        }

        /// <summary>
        /// 获取 最新发消息的用户
        /// </summary>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public string[] getNewMsgUsers()
        {
            List<string> msgUsers = new List<string>();
            if (currentUserID != "")
            {
                string sqlText = "dbo.[GetNewMsgUsers] '" + currentUserID + "'";
                DataSet dSet = SqlHelper.Instance.ExecuteQuery(sqlText);
                if (dSet.Tables.Count > 0 && dSet.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dRow in dSet.Tables[0].Rows)
                        msgUsers.Add(dRow["user_from"].ToString());
                }

            }
            return msgUsers.ToArray();
        }

        [AjaxPro.AjaxMethod]
        public void InsertNewMessage(string user_to, string content, int ispublic)
        {
            if (AliApi.appUserId != string.Empty)
            {
                cbChatInfo chatinfo = new cbChatInfo();
                chatinfo.Init(0);
                chatinfo.New();
                chatinfo.cbChatInfoId.Value = chatinfo.GetRecID().ToString();
                chatinfo.user_from.Value = currentUserID;
                chatinfo.user_to.Value = user_to;
                chatinfo.sendTime.Value = DateTime.Now;
                chatinfo.ispublic.Value = ispublic;
                chatinfo.content.Value = content;
                chatinfo.Status.Value = RuleCode.Enum.ChatInfoStatus.None;
                chatinfo.Insert();

                //判断是否为陌生人发送消息
                string sqlText = " exec updateGroupRelationShip @user_to='" + user_to + "',@user_from='" + AliApi.appUserId + "'";
                SqlHelper.Instance.ExecuteNonQuery(CommandType.Text, sqlText);
            }
        }

        /// <summary>
        /// 获取动态聊天对象列表
        /// </summary>
        /// <returns></returns>
        [AjaxPro.AjaxMethod ]
        public string getDynamicChatRoomUserTos()
        {
            string result = string .Empty ;
            cbuser user = cbuser.Find("cbuserid='"+AliApi.appUserId +"'");
            if (user.Count > 0)
            {                                                            
                result = user.DynRoomUserTo_Recids.Value;
            }
            return result;
        }

        [AjaxPro.AjaxMethod ]
        public string removeDynamicChatRoomUserTos()
        {
            string result = string.Empty;
            cbuser user = cbuser.Find("cbuserid='" + AliApi.appUserId + "'");
            if (user.Count > 0)
            {
                user.DynRoomUserTo_Recids.Value = "";
                user.Modify();
            }
            else
            {
                result = "未登录";
            }
            return result;
        }
        #endregion
    }
}
